﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.Data;
using System.Data.SqlClient;

namespace ClassLibrary.DAO
{
    public class TblOrderDAO
    {
        private DBService _db = null;
        public TblOrderDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public bool InsUpdOrder(TblOrder Order, int mode)
        {
            bool flag = false;
            try
            {

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetOrder(int id)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_TblOrder_Get",
                    new string[] { "@OrderId" }
                    , new object[] { id }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool Insert(TblOrder order, List<TblOrderDetail> lstDetail, out int OrderID)
        {
            OrderID = 0;
            bool flag = false;
            try
            {
                //StringBuilder str = new StringBuilder();
                //str.AppendLine("begin tran");
                //str.AppendLine("declare @OrderDetails order_list_tbltyp");

                DataTable dt = _db.ExeSP_DataTable("sp_Order_Ins",
                   new string[] { "@ShipTo", "@BillTo", "@ShippingMethodID", "@Memo", "@AccountID", "@Total", "@DeliveryFee", "@TransactionID" },
                   new object[] { 
                                        Code.FixStringNull(order.ShipTo),
                                        Code.FixStringNull(order.BillTo),
                                        Code.FixIntNull(order.ShippingMethodID).ToString(), 
                                        Code.FixStringNull(order.Memo),
                                        Code.FixIntNull(order.AccountID).ToString(),
                                        Code.FixFloatNull(order.Total).ToString(),
                                        Code.FixFloatNull(order.DeliveryFee).ToString(),
                                        Code.FixStringNull(order.TransactionId).ToString(),
                    }, CommandType.StoredProcedure);

                foreach (TblOrderDetail orderdetail in lstDetail)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        orderdetail.TotalAmount = orderdetail.Quantity * orderdetail.Price;
                       // orderdetail.TaxExAmount = orderdetail.Price;
                        //orderdetail.TaxInAmount =orderdetail.
                        _db.ExeSP_DataTable("TblOrderDetail_Insert",
                          new string[] { "@OrderID","@ItemID", "@TaxExAmount", "@TaxInAmount", "@TaxCodeID", "@Quantity", "@Price", "@TotalAmount" },
                          new object[] { dr["OrderID"],
                              Code.FixIntNull(orderdetail.ItemID),
                                     orderdetail.TaxExAmount,
                                     orderdetail.TaxInAmount,
                                    Code.FixIntNull(orderdetail.TaxCodeID),
                                    Code.FixIntNull(orderdetail.Quantity), 
                                    orderdetail.Price,
                                    orderdetail.TotalAmount},
                                         CommandType.StoredProcedure);

                        OrderID = Convert.ToInt32(dr["OrderID"].ToString()) ;
                    }
                   
                }
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public DataTable GetOrderLst(string p_OrderDateFrom, string p_OrderDateTo, string p_Email)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_Order_Lst",
                    new string[] { "@p_OrderDateFrom", "@p_OrderDateTo", "@p_ItemNumber", "@p_Email" },
                    new object[] { p_OrderDateFrom, p_OrderDateTo, "", p_Email }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public DataTable GetOrderList(string p_OrderDateFrom, string p_OrderDateTo, string p_Email, int status)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_Order_List",
                    new string[] { "@p_OrderDateFrom", "@p_OrderDateTo", "@p_ItemNumber", "@p_Email" , "@p_Status"},
                    new object[] { p_OrderDateFrom, p_OrderDateTo, "", p_Email, status }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Update(int Status, int OrderID)
        {
            DataTable dt = null;
            try
            {
                
                dt = _db.ExeSP_DataTable("Update TblOrder set Status= @Status where OrderID = @OrderID",
                    new string[] { "@Status", "@OrderID" },
                    new object[] { Status, OrderID }, CommandType.Text);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

    }
}
